<template>
  <view >
    <view class="goods-list">
      <view v-for="(goods,i) in goodsList" :key="i" @click="gotoDetail(goods)">
        <my-goods :goods="goods"></my-goods>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        queryObj: {
          //查询关键词
          query:'',
          //商品id
          cid:'',
          //页码
          pagenum:1,
          //每页数据
          pagesize:10
          
        },
        goodsList:[],
        total:0,
        //节流阀
        isloading:false
      };
    },
    onLoad(option){
      this.queryObj.query = option.query || ''
      this.queryObj.cid = option.cid || ''
      this.getGoodsList()
    },
    methods:{
      //获取数据
      async getGoodsList(cb){
        this.isloading = true
        const {data:res} = await uni.$http.get('/api/public/v1/goods/search',  this.queryObj)
        this.isloading = false
        cb && cb()
        if (res.meta.status !== 200) return nui.$showMsg()
        this.goodsList = [...this.goodsList,...res.message.goods]
        this.total = res.message.total
        if(this.total===0){
          uni.showModal({
                  				title: '提示',
                  				content: '没有商品信息',
                          showCancel:false,
                  				success: ()=>{
                            uni.navigateBack()
                          }
                  			});
        }
      },
      //触底事件
      onReachBottom(){
        if(this.queryObj.pagenum * this.queryObj.pagesize >= this.total)
        return uni.$showMsg('没有更多商品') 
        if(this.isloading)return
        this.queryObj.pagenum += 1
        this.getGoodsList()
      },
      onPullDownRefresh(){
        this.queryObj.pagenum = 1
        this.total = 0
        this.isloading = false
        this.goodsList = []
        this.getGoodsList(()=> uni.stopPullDownRefresh())
      },
      // 跳转
      gotoDetail(item){
        uni.navigateTo({
          url:'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
        })
      }
      
      
    }
  }
</script>

<style lang="scss">

</style>
